import SchemaMeta from './schema-meta';
import type { PropsParserProps, PropsParserConstructorProps, SourceCode } from '../types';

export default class PropsParser extends SchemaMeta implements PropsParserProps {
  private props: SourceCode['props'];
  constructor(props: PropsParserConstructorProps) {
    super(props);
  }

  async parse(): Promise<SourceCode['props']> {
    const attrs = this.schema.props;
    if (attrs && typeof attrs === 'object') {
      const propKeys = Object.keys(attrs).filter(Boolean);
      if (propKeys.length) {
        this.props = propKeys.reduce<string[]>((acc, key) => {
          const v = attrs[key];
          switch (typeof v) {
            case 'string':
              acc.push(`${key}="${attrs[key]}"`);
              break;
            default:
              acc.push(`${key}={${JSON.stringify(attrs[key])}}`);
          }
          return acc;
        }, []);
      }
    }

    return this.props;
  }
}
